/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 */

package com.xci.edu.core.service;

import com.github.lvyanyang.annotation.OperateLog;
import com.github.lvyanyang.core.BaseService;
import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.PageList;
import com.github.lvyanyang.core.XCI;
import com.xci.edu.core.dao.ExamPaperDetailsDao;
import com.xci.edu.core.entity.ExamPaperDetails;
import com.xci.edu.core.filter.ExamPaperDetailsFilter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 试卷试题明细服务
 * @author 马星俊 maxingjun@xci96716.com
 * @since 2020-11-25 10:52:08
 */
@Service
public class ExamPaperDetailsService extends BaseService {
    /** 试卷试题明细数据层对象 */
    @Resource private ExamPaperDetailsDao examPaperDetailsDao;

    /**
     * 新建试卷试题明细
     * @param entity 试卷试题明细实体
     */
    @OperateLog(msg = "新建试卷试题明细", module = "试卷试题明细")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult insert(ExamPaperDetails entity) {
        return save(entity, true);
    }

    /**
     * 修改试卷试题明细
     * @param entity 试卷试题明细实体
     */
    @OperateLog(msg = "修改试卷试题明细", module = "试卷试题明细")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult update(ExamPaperDetails entity) {
        return save(entity, false);
    }

    /**
     * 修改试卷试题明细状态
     * @param ids    试卷试题明细主键字符串
     * @param status 试卷试题明细状态
     */
    @OperateLog(msg = "修改试卷试题明细状态", module = "试卷试题明细")
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(@NotBlank(message = "请指定试卷试题明细主键字符串") String ids, @NotNull(message = "请指定试卷试题明细状态") Boolean status) {
        XCI.forEachIds(ids, id -> {
            examPaperDetailsDao.updateStatus(id, status);
        });
    }

    /**
     * 批量保存试卷试题明细
     * @param entities 试卷试题明细实体集合
     */
    @OperateLog(msg = "修改试卷试题明细", module = "试卷试题明细")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult batchSave(List<ExamPaperDetails> entities) {
        for (ExamPaperDetails entity : entities) {
//            if (entity.get() == null || !examPaperDetailsDao.existById(entity.get())) {
//                //无主键或者主键在数据库中不存在,则新建;
//                save(entity, true).ifFailThrow();
//            } else {
//                //其他情况,则修改
//                save(entity, false).ifFailThrow();
//            }
            save(entity, true).ifFailThrow();
        }
        return BoolResult.success();
    };


    /**
     * 根据主键查询单个试卷试题明细
     * @param id 试卷试题明细主键
     * @return 返回试卷试题明细实体
     */
    public ExamPaperDetails
     selectById(@NotNull(message = "请指定试卷试题明细主键") String id) {
        return examPaperDetailsDao.selectById(id);
    }

    /**
     * 查询试卷试题明细列表
     * @param filter 过滤条件
     * @return 返回试卷试题明细列表
     */
    public List<ExamPaperDetails> selectList(ExamPaperDetailsFilter filter) {
        return examPaperDetailsDao.selectList(filter);
    }

    /**
     * 查询试卷试题明细分页列表
     * @param filter 过滤条件
     * @return 返回试卷试题明细分页列表
     */
    public PageList<ExamPaperDetails> selectPageList(ExamPaperDetailsFilter filter) {
        return PageList.of(examPaperDetailsDao.selectPageList(filter));
    }

    /**
     * 保存数据,在insert和update之前和之后的校验和处理
     * @param entity  试卷试题明细实体
     * @param created 是否新建
     */
    private BoolResult save(ExamPaperDetails entity, boolean created) {
        //新建时如果主键未指定,自动设置主键
        //XCI.ifTrueAction(XCI.invalidId(entity.get(), created), () -> entity.set(XCI.nextId()));
        //如果简拼为空,设置简拼
        //XCI.ifBlankAction(entity.getSpell(), () -> entity.setSpell(XCI.getSpell(entity.getName())));

        //检查试卷试题明细编码是否存在
        //if (examPaperDetailsDao.existxByCode(entity.getCode(), XCI.getExcludeId(entity.get(), created))) {
        //    return BoolResult.fail(XCI.format("试卷试题明细编码[{}]已经存在", entity.getCode()));
        //}

        //更新数据库
        if (created) {
            examPaperDetailsDao.insert(entity);
            //logService.saveInsertHistoryLogAsync(entity.get(), entity);
        } else {
            //var before = examPaperDetailsDao.selectById(entity.get());
            examPaperDetailsDao.update(entity);
            //logService.saveUpdateHistoryLogAsync(entity.get(), before, entity);
        }
        return BoolResult.success();
    }
}